<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/date.class.php');
    require_once($config['app_dir'] . '/includes/db.class.php');
    //---------------------------------------------------------------------------------------------------

    //---------------------------------------------------------------------------------------------------
    // objLogRecord
    //---------------------------------------------------------------------------------------------------
    abstract class objLogRecord {
           protected $data = false;
           protected $timestamp = false;

        public function __construct($aData) {
           $this->data = $aData;
           $this->timestamp = DateConvert::Date('U');//adodb_date('U');
        }//constructor

        public function isEmpty() {
            return empty($this->data) || false==$this->data;
        } //isEmpty

        abstract public function Render(); //Render

    } //class LogRecord

    //---------------------------------------------------------------------------------------------------
    // objLog
    //---------------------------------------------------------------------------------------------------
    abstract class objLog {
            public $max_items = 10;

            protected $items = array();

        public function __construct() {
            $this->Open();
        } //constructor

        public function __destruct() {
            $this->Flush();
            $this->Close();
        } //destructor

        public function Clear() {
            unset($this->items);
            $this->items = array();
        } //Clear

        public function isEmpty() {
            return empty($this->items) || false==$this->items;
        } //isEmpty

        public function Add(objLogRecord $Record) {
            $this->items[] = $Record->Render();
            if( count($this->items) > $this->max_items ) $this->Flush();
        } //Add

        public function AddLine() {
            $this->items[] = "\n====================================================================\n";
            if( count($this->items) > $this->max_items ) $this->Flush();
        } //AddLine

        abstract public function Flush(); //Flush

        abstract protected function Open();  //Open

        abstract protected function Close();  //Close

    } //class Log

    //---------------------------------------------------------------------------------------------------
    // objTextLog
    //---------------------------------------------------------------------------------------------------
    class objTextLog extends objLog {
            protected $file_name = false;
            protected $file_handle = false;

        public function __construct($aFileName) {
          global $config;
            $this->file_name = $config['log']['path'] . $aFileName . DateConvert::Date('Ymd')/*adodb_date('Ymd')*/ . '.log';
            if( !is_dir($config['log']['path']) ) mkdir($config['log']['path']);
            parent::__construct();
        } //constructor

        protected function Open() {
            $this->file_handle = fopen($this->file_name, "a");
        } //Open

        protected function Close() {
            fclose($this->file_handle);
        } //Close

        public function Flush() {
            if( !$this->isEmpty && fwrite($this->file_handle, implode("\n", $this->items)) )    $this->Clear();
        } //Flush

    } //class objTextLog

    //---------------------------------------------------------------------------------------------------
    // objMailerLogRecord
    //---------------------------------------------------------------------------------------------------
    class objMailerLogRecord extends objLogRecord {

        public function __construct($aData) {
           $this->data = $aData;
           $this->timestamp = DateConvert::Date('r');//adodb_date('r');
        }//constructor

        public function Render() {
           $result =  '';
           $result .= "\n================================================================================\n";
           $result .= "Timestamp:\n" . $this->timestamp;
           $result .= "\n--------------------------------------------------------------------------------\n";
           $result .= " Message: \n" . $this->data['header'] . "\n" . $this->data['body'];
           $result .= "\n--------------------------------------------------------------------------------\n";
           $result .= ' Result - ' . $this->data['result'];
           return $result;
        } //Render

    } //class MailerLogRecord
//-------------------------------------------------------------------------------------------------------
?>
